﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.CMS.Controls
{
    public partial class SetupDisplayItem : AddEditItemControl
    {
        private void BuildCategory()
        {
            DataSet ds = Category.SelectAll(this.ModuleID);
            DataRow[] rootRows = ds.Tables[0].Select("ParentID = 0");

            foreach (DataRow row in rootRows)
            {
                ddlCategory.Items.Add(new ListItem(row["Title"].ToString(), row["ID"].ToString()));
                DataRow[] childRows = ds.Tables[0].Select("ParentID = " + row["ID"]);
                if (childRows.Length > 0)
                {
                    foreach (DataRow childRow in childRows)
                    {
                        ddlCategory.Items.Add(new ListItem("» " + childRow["Title"], childRow["ID"].ToString()));
                    }
                }
            }
        }

        protected string GetExpiredDate(object date)
        {
            DateTime d = Convert.ToDateTime(date);
            string result = d.ToString();
            if (d.Year == 9999)
            {
                result = "Không giới hạn";
            }
            return result;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BuildCategory();
                
            }
        }

        protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            gvItem.PageIndex = ddl.SelectedIndex;
            gvItem.DataBind();
        }

        protected void gvItem_DataBound(object sender, EventArgs e)
        {
            if (gvItem.Rows.Count > 0)
            {
                // Retrieve the pager row.
                GridViewRow bottomPagerRow = gvItem.BottomPagerRow;
                GridViewRow topPagerRow = gvItem.TopPagerRow;

                // Retrieve the DropDownList and Label controls from the row.
                DropDownList topPageList = (DropDownList)topPagerRow.Cells[0].FindControl("PageDropDownList");
                DropDownList bottomPageList = (DropDownList)bottomPagerRow.Cells[0].FindControl("PageDropDownList");
                Label topPageLabel = (Label)topPagerRow.Cells[0].FindControl("PageLabel");
                Label bottomPageLabel = (Label)bottomPagerRow.Cells[0].FindControl("PageLabel");

                if (topPageList != null && bottomPageList != null)
                {
                    for (int i = 0; i < gvItem.PageCount; i++)
                    {
                        int pageNumber = i + 1;
                        ListItem item = new ListItem(pageNumber.ToString());
                        if (i == gvItem.PageIndex)
                        {
                            item.Selected = true;
                        }

                        topPageList.Items.Add(item);
                        bottomPageList.Items.Add(item);
                    }
                }

                if (topPageLabel != null && bottomPageLabel != null)
                {
                    topPageLabel.Text = bottomPageLabel.Text = " / " + gvItem.PageCount;
                }
            }
        }

        protected void gvItem_PageIndexChanged(object sender, EventArgs e)
        {
            gvItem.SelectedIndex = -1;
        }

        protected void gvItem_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            hf.Value = e.CommandArgument.ToString();
            if (e.CommandName.ToUpper().Equals("ONE"))
            {
                NewsWindow.Attributes.Add("src", Global.GetApplicationPath() + "/Modules/ASPNETVN.PORTAL.Modules.CMS/Pages/UploadFile.aspx?mid=" + this.ModuleID + "&itemid=" + hf.Value);
                mpeUploadFile.Show();
            }
            Components.Item.SetupDisplayItem(Convert.ToInt32(e.CommandArgument), e.CommandName);
            gvItem.DataBind();
        }

        protected void sdsItem_Init(object sender, EventArgs e)
        {
            sdsItem.SelectParameters["CreatedBy"].DefaultValue = PortalSecurity.HasManageAuthorization(this.ModuleID) ? "%" : PortalSecurity.CurrentUser.Identity.Email;
        }

        protected void btnSaveUploadFile_Click(object sender, EventArgs e)
        {
           
        }
    }
}